//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：HDMAssetsLoad
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System.Collections;
using UnityEngine;
using YooAsset;
namespace HDMCode.Hotfix
{
    public static class HDMAssetsLoad
    {
        /// <summary>
        /// 绑定的资源包
        /// </summary>
        public static ResourcePackage assetsPackage { get; set; }
        public static ResourcePackage scriptPackagee { get; set; }

        /// <summary>
        /// 尝试从资源包加载
        /// </summary>
        /// <typeparam name="T">资源类型</typeparam>
        /// <param name="name">资源名称</param>
        /// <param name="handle">资源处理</param>
        /// <returns></returns>
        private static bool AssetHandle<T>(string name,ref AssetHandle handle) where T : Object
        {
            if (assetsPackage != null)
            {
                handle = assetsPackage.LoadAssetSync<T>(name);
                return true;
            }
            else
            {
                Debug.LogError($"无法初始化资源包 {name}");
                return false;
            }
        }

        /// <summary>
        /// 加载资源
        /// </summary>
        /// <typeparam name="T">资源类型</typeparam>
        /// <param name="name">资源名称</param>
        /// <returns></returns>
        public static T LoadAssets<T>(string name) where T : Object
        {
            AssetHandle handle = default;
            if (AssetHandle<T>(name,ref handle))
            {
                T assets = handle.AssetObject as T;
                return assets;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 加载GameObject对象
        /// </summary>
        /// <param name="name">资源名称</param>
        /// <returns></returns>
        public static GameObject LoadGameObject(string name,Transform parent = null) 
        {
            AssetHandle handle = default;
            AssetHandle<GameObject>(name, ref handle);
            if (parent!=null)
                return handle.InstantiateSync(parent, false);
            else
                return handle.InstantiateSync();
        }

        /// <summary>
        /// 加载场景
        /// </summary>
        /// <returns></returns>
        public static IEnumerator LoadScene(string sceneName,System.Action action=null,string packageName=null)
        {
            if (string.IsNullOrEmpty(packageName))
                packageName = assetsPackage.PackageName;
            var package = YooAssets.GetPackage(packageName);
            var sceneMode = UnityEngine.SceneManagement.LoadSceneMode.Single;
            SceneHandle handle = package.LoadSceneAsync(sceneName, sceneMode, false);
            yield return handle;
            action?.Invoke();
        }

        /// <summary>
        /// 加载字节资源
        /// </summary>
        /// <param name="name">资源名称</param>
        /// <returns></returns>
        public static IEnumerator LoadFile(string name,System.Action<byte[]> action)
        {
            RawFileHandle handle = scriptPackagee.LoadRawFileAsync(name);
            yield return handle;
            action?.Invoke(handle.GetRawFileData());
        }

        /// <summary>
        /// 卸载资源包中所有未使用用的资源
        /// </summary>
        /// <param name="packageName">包名</param>
        public static void UnloadUnusedAssets(string packageName = null)
        {
            if (string.IsNullOrEmpty(packageName))
                packageName = assetsPackage.PackageName;
            var package = YooAssets.GetPackage(packageName);
            package.UnloadUnusedAssets();
        }

        /// <summary>
        /// 强制卸载所有资源
        /// </summary>
        public static void UnloadAllAssets(string packageName = null)
        {
            if (string.IsNullOrEmpty(packageName))
                packageName = assetsPackage.PackageName;
            var package = YooAssets.GetPackage(packageName);
            package.ForceUnloadAllAssets();
        }

        /// <summary>
        /// 尝试卸载指定资源
        /// </summary>
        public static void TryUnloadUnusedAsset(string assetsName, string packageName=null)
        {
            if (string.IsNullOrEmpty(packageName))
                packageName = assetsPackage.PackageName;
            var package = YooAssets.GetPackage(packageName);
            package.TryUnloadUnusedAsset(assetsName);
        }

    }
}
